from time import sleep

from selenium import webdriver

'''
selenium常用关键字类：
1 元素定位
2 输入
3 点击
4 访问url
5 强制等待
6 关闭浏览器
'''


def open_browser(txt):
    """
    if type_ == 'Chrome':
        driver = webdriver.Chrome()
    elif type_ == 'Firefox':
        driver = webdriver.Firefox()
    else:
        driver = webdriver.Ie()
    return driver
    """
    try:
        # python的反射机制
        driver = getattr(webdriver, txt)()
    except Exception as e:
        print(e)
        driver = webdriver.Chrome()
    return driver


class Key:
    # 构造函数
    def __init__(self, txt):
        self.driver = open_browser(txt)

    # 元素定位，需要满足八种不通的元素定位方法
    def locator(self, name, value):
        return self.driver.find_element(name, value)

    # 点击
    def click(self, name, value):
        return self.locator(name, value).click()

    # 输入
    def input(self, name, value, txt):
        return self.locator(name, value).send_keys(txt)

    # 打开浏览器
    def open(self, txt):
        return self.driver.get(txt)

    # 关闭浏览器
    def quit(self):
        self.driver.quit()

    # 强制等待
    def sleep(self, txt):
        sleep(txt)